<template>
	<view width="dflex">
		<view class="p_absolute">
			<image 
				src="https://aikm.cn/static/welcome.png" 
				mode="aspectFill" 
				:style="'height: ' + screen_height + 'px; width: ' + screen_width + 'px;'"
			></image>
		</view>		
		<view class="dflex mt20 p_fix w100percent jcc">
			<text 
				class="p12 taligncenter manual"
				:style="'margin-top:' + text_top + 'px;'"
				@click="manual_select"
			>手动选择区域</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				screen_width : '',
				screen_height : '',
				text_top : ''
			}
		},
		onLoad() {			
			var sys = uni.getSystemInfoSync()
			this.screen_width = sys.windowWidth
			this.screen_height = sys.windowHeight
			this.text_top = (sys.windowHeight / 2) - 50 
			// console.log(this.screen_width, this.screen_height)
			uni.getStorage({
				key:'Domain',
				success() {
					uni.redirectTo({
						url:'/pages/index/index'
					})
				},
				fail:()=> {		
					this.requestLocation()
				}
			})
		},
		methods: {		
			requestLocation() {
				uni.showModal({
					title:'提示',
					content:'请同意获取位置信息，以方便为您提供所在城市的服务。',
					success: (res) => {
						if(res.confirm) {							
							this.getLocationAuth()
						}
						if(res.cancel) {
							uni.redirectTo({
								url:'/packageShop/location/location'
							})
						}
					}
				})
			},
			gotoIndex() {				
				uni.redirectTo({
					url:'/pages/index/index'
				})
			},
			//获取定位
			getLocationAuth(){
				uni.getLocation({
					type: 'gcj02',
					success:(res)=> {
						// console.log('that.longitude', that.longitude)
						let key = '4MBBZ-Y5NKR-7SLWB-WW7XD-U66TE-YEBRD';  //申请地址：https://lbs.qq.com/dev/console/application/mine
						
						uni.request({
								url:'https://apis.map.qq.com/ws/geocoder/v1/?location='+res.latitude+','+res.longitude+'&key='+key,
								// url:'https://apis.map.qq.com/ws/geocoder/v1/?location=39.466729,75.992857&key='+key,
								method:"GET",
								success:(ress)=> {
									// console.log('resssssssssssss',ress.data)
									let cityCode = ress.data.result.ad_info.city_code	
									let cityName = ress.data.result.address_component.city
									console.log('cityCode1',cityCode)
									uni.setStorage({
										key:'City_Code',
										data:cityCode
									})	
									uni.setStorage({
										key:'City_Name',
										data:cityName
									})
									switch(cityCode) {
										case '156530100' :
										uni.setStorage({
											key:'Domain',
											data:'https://aikm.cn',
											// data:'http://aikm.cc',
											success:()=> {												
												this.gotoIndex()
											}
										})
										break
										
										case '156653100' :
										uni.setStorage({
											key:'Domain',
											data:'https://ks.aikm.cn',
											success:()=> {												
												this.gotoIndex()
											}
										})
										break
										
										default :
										this.withoutCity()
										break										
									}
								}
						})
					},
					fail(res) {
						uni.navigateTo({
							url:'/packageShop/location/location'
						})
					}
				})
			},
			manual_select() {
				uni.navigateTo({
					url:'/packageShop/location/location'
				})
			},
			withoutCity() {
				uni.showModal({
					title:'提示',
					content:'您所在的城市还没有同城趣服务商，请进入主站或申请入驻服务商',
					cancelText:'申请入驻',
					confirmText:'进入主站',
					success(res) {
						uni.setStorage({
							key:'Domain',
							data:'https://aikm.cn'
						})
						if(res.confirm) {
							uni.navigateTo({
								url:'/pages/index/index'
							})
						}
						if(res.cancel) {
							uni.navigateTo({
								url:'/pages/about/about'
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.manual {
		border: 2px solid #333;
		padding: 5px 9px;
		border-radius: 50px;
	}
</style>
